package kr.hkit.androidcomplete02.ch19;

import kr.hyunist.androidcomplete01.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LongTime3Ex extends Activity {
	int mValue;
	TextView mText;
	ProgressDialog mProgress;
	boolean mQuit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_upload);

		mText = (TextView) findViewById(R.id.ch19_uploadtv);
	}

	@SuppressWarnings("deprecation")
	public void mOnClick(View v) {
		mValue = 0;
		showDialog(0);
		mQuit = false;
		mHandler.sendEmptyMessage(0);
	}
	
	@SuppressWarnings("deprecation")
	protected android.app.Dialog onCreateDialog(int id) {
		switch(id){
		case 0:
			mProgress = new ProgressDialog(this);
			mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgress.setTitle("Updating");
			mProgress.setMessage("Wait...");
			//mProgress.setCancelable(true);
			mProgress.setButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mQuit = true;
					mProgress.dismiss();
				}
				
			});
			return mProgress;
		}
		return null;
	}
	
	Handler mHandler = new Handler() {

		@SuppressWarnings("deprecation")
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mValue++;
			mText.setText(Integer.toString(mValue));
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {}

			if (mValue < 100 && mQuit == false) {
				mProgress.setProgress(mValue);
				mHandler.sendEmptyMessage(0);
			} else {
				dismissDialog(0);
			}

		}

	};



}
